import { _decorator, Color, Component, MotionStreak, Node, RigidBody2D, Sprite, v2, Vec2, Vec3 } from 'cc';
import { TriangleManager } from './TriangleManager';
import { PoolManager } from './PoolManager';
import { EventManager, MyEvent } from './EventManager';
const { ccclass, property } = _decorator;

@ccclass('TriangleItem')
export class TriangleItem extends Component {

    @property(Sprite)
    Icon: Sprite = null;

    @property(MotionStreak)
    Trail: MotionStreak = null;

    @property(RigidBody2D)
    RigidBody: RigidBody2D = null;

    @property
    MaxForce: number = 500;

    private _isFree: boolean = false;
    private _isPause: boolean = false;
    private _curLinearVelocity: Vec2 = new Vec2();

    init(parent: Node, worldPos: Vec3) {
        this.node.setParent(parent);
        this.node.setWorldPosition(worldPos);

        this._isFree = false;

        //设置颜色
        var color: Color = this.getRandomBrightColor();
        this.Icon.color = color;
        this.Trail.color = color;
        this.applyRandomForce();
    }

    protected update(dt: number): void {
        if (this._isPause) return;
        if (TriangleManager.Instance.checkOut(this.node.getWorldPosition())) {
            TriangleManager.Instance.removeItem(this.node);
        }
    }

    free() {
        if (this._isFree) return;
        this._isFree = true;
        TriangleManager.Instance.addItems(3);
    }

    //获取比较亮的颜色
    getRandomBrightColor(): Color {
        let r = Math.floor(128 + Math.random() * 128);
        let g = Math.floor(128 + Math.random() * 128);
        let b = Math.floor(128 + Math.random() * 128);
        return new Color(r, g, b, 255);
    }


    applyRandomForce() {
        if (!this.RigidBody) return;

        // 生成一个随机方向的单位向量
        let angle = Math.random() * Math.PI * 2;
        // let forceMagnitude = Math.random() * this.MaxForce;
        let forceMagnitude = this.MaxForce;

        let forceX = Math.cos(angle) * forceMagnitude;
        let forceY = Math.sin(angle) * forceMagnitude;

        let force = v2(forceX, forceY);

        // 施加力，点是刚体的中心点(默认)
        const point: Vec2 = new Vec2();
        this.RigidBody.getWorldCenter(point);
        this.RigidBody.applyForce(force, point, true);

        // 如果想用冲量，可以用applyImpulse：
        // this.rigidBody.applyImpulse(force, this.rigidBody.getWorldCenter(), true);
    }

    apply(force: Vec2) {
        // 施加力，点是刚体的中心点(默认)
        this.RigidBody.gravityScale = 1;

        const point: Vec2 = new Vec2();
        this.RigidBody.getWorldCenter(point);
        this.RigidBody.applyForce(force.multiplyScalar(this.MaxForce), point, true);
    }

    private pause() {
        this._isPause = !this._isPause;
        if (this._isPause) {
            this._curLinearVelocity = this.RigidBody.linearVelocity;
            this.RigidBody.linearVelocity = Vec2.ZERO;
            this.RigidBody.gravityScale = 0;
        } else {
            this.RigidBody.gravityScale = 2;
            this.RigidBody.linearVelocity = this._curLinearVelocity;
        }
    }

    protected onEnable(): void {
        EventManager.on(MyEvent.PAUSE, this.pause, this);
    }

    protected onDisable(): void {
        EventManager.off(MyEvent.PAUSE, this.pause, this);
    }
}


